﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

public partial class UpdateTypeOfMail : System.Web.UI.Page
{
    int MTID;
    SqlConnection cnn;
    SqlCommand cmd;
    SqlDataReader dread;
    protected void Page_Load(object sender, EventArgs e)
    {
        //Check user login or not
        if (Session["Username"] == null)
        {
            Response.Redirect("Login.aspx");
        }
        else
            if (Session["RoleName"].ToString() == "Employee")
            {
                Response.Redirect("ValidateUser.aspx");
            }

        MTID = Int32.Parse(Request.QueryString["MTID"].ToString());
        lblMTID.Text = MTID.ToString();
        cnn = new SqlConnection(ConfigurationManager.ConnectionStrings["OPOConnectionString"].ToString());
        cnn.Open();
        cmd = new SqlCommand("Select * from TypeOfMail where MTypeID = '" + MTID + "'", cnn);
        dread = cmd.ExecuteReader();
        if (!IsPostBack)
        {
            if (dread.Read())
            {
                txtMTName.Text = dread.GetString(1);
                txtMTDes.Text = dread.GetString(2);
            }
        }
    }
    protected void imgbUpdate_Click(object sender, ImageClickEventArgs e)
    {
        dread.Close();
        cmd = new SqlCommand("Update TypeOfMail Set MTypeName = @MTypeName, Description = @Description where MTypeID = '" + MTID + "'", cnn);

        cmd.Parameters.Add(new SqlParameter("@MTypeName", SqlDbType.NVarChar, 50));
        cmd.Parameters["@MTypeName"].Value = txtMTName.Text;
        cmd.Parameters.Add(new SqlParameter("@Description", SqlDbType.NVarChar, 1000));
        cmd.Parameters["@Description"].Value = txtMTDes.Text;

        cmd.ExecuteNonQuery();

        ScriptManager.RegisterStartupScript(this, this.GetType(), "key", "ShowMessage()", true);
    }
    protected void imgbReset_Click(object sender, ImageClickEventArgs e)
    {
        if (dread.Read())
        {
            txtMTName.Text = dread.GetString(1);
            txtMTDes.Text = dread.GetString(2);
        }
    }

    protected void imgbBack_Click(object sender, ImageClickEventArgs e)
    {
        Response.Redirect("TypeOfMail_Management.aspx");
    }
}